<?php
session_start ();

$_SESSION ['pathway'] = '..';
$_SESSION ['pathway_js'] = '/genetiquespataxeurospa';
include ($_SESSION ['pathway'] . "/header.php");
require_once ($_SESSION ['pathway'] . "/commun/ouvre_base.php");

/*
 * print_r($_GET);
 * print_r($_FILES);
 * //
 */

if (! isset ( $_GET ['from'] ) or $_GET ['from'] != 'entry') { // c'est une entrée donc puisqu'on garde ancien onglet, pas besoin de fil d'ariane
	?><p>
	<a href="<?php echo $_SESSION['pathway']; ?>/index.php">Home</a> > <a
		href="<?php echo $_SESSION['pathway']; ?>/genetiquespataxeurospa/consultation.php">Export
		Genetics</a> > Request results
</p><?php
}

if (! isset ( $_SESSION ['id'] ) or $_SESSION ['acces_genetique'] < 1) {
	echo "<table class='principale'><tr><td>Session expired</td></tr>";
	echo "<tr><td><a href='" . $_SESSION ['pathway'] . "/index.php'>Start</a></td></tr></table>";
} else {
	
	// supprimer fiche
	if (isset ( $_GET ['delete'] ) and isset ( $_GET ['table'] )) {
		$query_deletefiche = "DELETE FROM `" . $db . "`.`" . $_GET ['table'] . "` WHERE `" . $_GET ['table'] . "`.`index` = " . $_GET ['delete'];
		mysql_query ( $query_deletefiche );
		$table = $_GET ['table'];
	}
	
	// modifications
	if (isset ( $_GET ['edit'] ) and isset ( $_GET ['table'] )) {
		$_SESSION ['edit'] = $_GET ['edit'];
		require_once ("../genetiquespataxeurospa/requetes/remplissage_" . $_GET ['table'] . ".php");
		// require_once("../genetiquespataxeurospa/requetes/remplissage_sample.php");
	}
	
	$liens = array (
			'linkage' => array (
					'18',
					'31' 
			),
			'screening' => array (
					'28',
					'42' 
			),
			'wholegenomeanalysis' => array (
					'17',
					'30' 
			) 
	);
	
	if (isset ( $_POST ['code1'] ) or isset ( $_POST ['code2'] ) or isset ( $_POST ['code3'] ) or isset ( $_POST ['code4'] ) or isset ( $_POST ['code5'] ) and isset ( $_POST ['submit'] ) and $_POST ['submit'] == 'yes') {
		$code1 = $_POST ['code1'];
		$code2 = $_POST ['code2'];
		$code3 = $_POST ['code3'];
		$code4 = $_POST ['code4'];
		$code5 = $_POST ['code5'];
		$ind = $_POST ['code1'] . '-' . $_POST ['code2'] . '-' . $_POST ['code3'] . '-' . $_POST ['code4'] . '-' . $_POST ['code5']; // si besoin lors de la suppression
	} else if (isset ( $_GET ['ind'] )) {
		$ind = $_GET ['ind'];
		$array_famille = array ();
		$array_famille = explode ( '-', $ind );
		if (isset ( $array_famille [0] )) {
			$code1 = $array_famille [0];
		}
		if (isset ( $array_famille [1] )) {
			$code2 = $array_famille [1];
		}
		if (isset ( $array_famille [2] )) {
			$code3 = $array_famille [2];
		}
		if (isset ( $array_famille [3] )) {
			$code4 = $array_famille [3];
		}
		if (isset ( $array_famille [4] )) {
			$code5 = $array_famille [4];
		}
	} else {
		$code1 = '';
		$code2 = '';
		$code3 = '';
		$code4 = '';
		$code5 = '';
	}
	
	if (isset ( $_POST ['codeexterne'] ) and $_POST ['codeexterne'] != '') {
		$codeexterne = $_POST ['codeexterne'];
	}
	if (isset ( $_POST ['codegenethon'] ) and $_POST ['codegenethon'] != '') {
		$codegenethon = $_POST ['codegenethon'];
	}
	if (isset ( $_POST ['neurologist'] ) and $_POST ['neurologist'] != '') {
		$neurologist = $_POST ['neurologist'];
	}
	if (isset ( $_POST ['centre'] ) and $_POST ['centre'] != '') {
		$centre = $_POST ['centre'];
	}
	if (isset ( $_POST ['date'] ) and $_POST ['date'] != '') {
		$date = $_POST ['date'];
	}
	if (isset ( $_POST ['user'] ) and $_POST ['user'] != '') {
		$user = $_POST ['user'];
	} else {
		$user = '';
	}
	if (isset ( $_POST ['validation'] ) and $_POST ['validation'] != '') {
		$validation = $_POST ['validation'];
	}
	if (isset ( $_POST ['afaire'] ) and $_POST ['afaire'] != '') {
		$afaire = $_POST ['afaire'];
	}
	
	if ($code1 != '' or $code2 != '' or $code3 != '' or $code4 != '' or $code5 != '' or // on vient de consultation.php
isset ( $_SESSION ['requete_linkage'] ) or isset ( $_SESSION ['requete_screening'] ) or isset ( $_SESSION ['requete_wholegenomeanalysis'] )) { // on vient d'edit donc la requete est dans session
		
		if (isset ( $_POST ['submit'] ) and $_POST ['submit'] == ' Global search ') {
			$tables = array (
					'linkage',
					'screening',
					'wholegenomeanalysis' 
			);
		} else if (isset ( $_POST ['submit'] ) and $_POST ['submit'] == ' Linkage view ') {
			$tables = array (
					'linkage' 
			);
		} else if (isset ( $_POST ['submit'] ) and $_POST ['submit'] == ' Screening view ') {
			$tables = array (
					'screening' 
			);
		} else if (isset ( $_POST ['submit'] ) and $_POST ['submit'] == ' WGA view ') {
			$tables = array (
					'wholegenomeanalysis' 
			);
		} else if (isset ( $_GET ['from'] ) and $_GET ['from'] == 'modif' or isset ( $_GET ['delete'] ) and $_GET ['delete'] != '') {
			$tables = $_SESSION ['tables'];
		} else {
			$tables = array (
					'linkage',
					'screening',
					'wholegenomeanalysis' 
			);
		}
		$_SESSION ['tables'] = $tables;
		
		foreach ( $tables as $table ) {
			if ($table == 'linkage') {
				if (isset ( $_POST ['statutlinkage'] ) and $_POST ['statutlinkage'] != '') {
					$statutlinkage = $_POST ['statutlinkage'];
				}
				if (isset ( $_POST ['locusteste'] ) and $_POST ['locusteste'] != '') {
					$locusteste = $_POST ['locusteste'];
				}
				if (isset ( $_POST ['linkage'] ) and $_POST ['linkage'] != '') {
					$linkage = $_POST ['linkage'];
				}
				if (isset ( $_POST ['modelinkage'] ) and $_POST ['modelinkage'] != '') {
					$modelinkage = $_POST ['modelinkage'];
				}
				if (isset ( $_POST ['diagnosticlinkage'] ) and $_POST ['diagnosticlinkage'] != '') {
					$diagnosticlinkage = $_POST ['diagnosticlinkage'];
				}
			}
			
			if ($table == 'screening') {
				if (isset ( $_POST ['statutscreening'] ) and $_POST ['statutscreening'] != '') {
					$statutscreening = $_POST ['statutscreening'];
				}
				if (isset ( $_POST ['gene'] ) and $_POST ['gene'] != '') {
					$gene = $_POST ['gene'];
				}
				if (isset ( $_POST ['transcript'] ) and $_POST ['transcript'] != '') {
					$transcript = $_POST ['transcript'];
				}
				if (isset ( $_POST ['modescreening'] ) and $_POST ['modescreening'] != '') {
					$modescreening = $_POST ['modescreening'];
				}
				if (isset ( $_POST ['techniquescreening'] ) and $_POST ['techniquescreening'] != '') {
					$techniquescreening = $_POST ['techniquescreening'];
				}
				if (isset ( $_POST ['cosegregation'] ) and $_POST ['cosegregation'] != '') {
					$cosegregation = $_POST ['cosegregation'];
				}
				if (isset ( $_POST ['diagnosticscreening'] ) and $_POST ['diagnosticscreening'] != '') {
					$diagnosticscreening = $_POST ['diagnosticscreening'];
				}
			}
			
			if ($table == 'wholegenomeanalysis') {
				if (isset ( $_POST ['statutwga'] ) and $_POST ['statutwga'] != '') {
					$statutwga = $_POST ['statutwga'];
				}
				if (isset ( $_POST ['techniquewga'] ) and $_POST ['techniquewga'] != '') {
					$techniquewga = $_POST ['techniquewga'];
				}
				if (isset ( $_POST ['diagnosticwga'] ) and $_POST ['diagnosticwga'] != '') {
					$diagnosticwga = $_POST ['diagnosticwga'];
				}
			}
			
			$nom_requete = 'requete_' . $table;
			if (isset ( $_SESSION [$nom_requete] ) and $_SESSION [$nom_requete] != '') {
				$requete_finale = $_SESSION [$nom_requete];
			} else {
				if (isset ( $_SESSION ['requete_' . $table] )) {
					unset ( $_SESSION ['requete_' . $table] );
				}
				$requete_finale = "SELECT *
				FROM `" . $table . "` WHERE ";
				
				$requete = '';
				if ($code1 != '' and $code1 != '*') {
					$requete .= " AND `code1` LIKE '%" . $code1 . "%'";
				}
				if ($code2 != '') {
					$requete .= " AND `code2` LIKE '%" . $code2 . "%'";
				}
				if ($code3 != '') {
					$requete .= " AND `code3` LIKE '%" . $code3 . "%'";
				}
				if ($code4 != '') {
					$requete .= " AND `code4` LIKE '%" . $code4 . "%'";
				}
				if ($table != 'linkage') {
					if ($code5 != '') {
						$requete .= " AND `code5` LIKE '%" . $code5 . "%'";
					}
				}
				if (isset ( $codeexterne ) and $codeexterne != '') {
					$requete .= " AND `codeexterne` LIKE '%" . $codeexterne . "%'";
				}
				if (isset ( $codegenethon ) and $codegenethon != '') {
					$requete .= " AND `codegenethon` LIKE '%" . $codegenethon . "%'";
				}
				if (isset ( $neurologist ) and $neurologist != '') {
					$requete .= " AND `neurologist` LIKE '%" . $neurologist . "%'";
				}
				if (isset ( $date ) and $date != '') {
					$requete .= " AND `date` LIKE '%" . $date . "%'";
				}
				if (isset ( $centre ) and $centre != '') {
					$requete .= " AND `centre` LIKE '%" . $centre . "%'";
				}
				if (isset ( $user ) and $user != '') {
					$requete .= " AND `user` LIKE '%" . $user . "%'";
				}
				if (isset ( $validation ) and $validation != '') {
					$requete .= " AND `validation` LIKE '%" . $validation . "%'";
				}
				if (isset ( $afaire ) and $afaire != '') {
					$requete .= " AND `afaire` LIKE '%" . $afaire . "%'";
				}
				
				if ($table == 'linkage') {
					if (isset ( $statutlinkage ) and $statutlinkage != '') {
						$requete .= " AND `statut` LIKE '%" . $statutlinkage . "%'";
					}
					if (isset ( $locusteste ) and $locusteste != '') {
						$requete .= " AND `locusteste` LIKE '" . $locusteste . "'";
					}
					if (isset ( $linkage ) and $linkage != '') {
						$requete .= " AND `linkage` LIKE '%" . $linkage . "%'";
					}
					if (isset ( $modelinkage ) and $modelinkage != '') {
						$requete .= " AND `modelinkage` LIKE '%" . $modelinkage . "%'";
					}
					if (isset ( $diagnosticlinkage ) and $diagnosticlinkage != '') {
						$requete .= " AND `diagnosticlinkage` LIKE '%" . $diagnosticlinkage . "%'";
					}
				}
				
				if ($table == 'screening') {
					if (isset ( $statutscreening ) and $statutscreening != '') {
						$requete .= " AND `statut` LIKE '%" . $statutscreening . "%'";
					}
					if (isset ( $gene ) and $gene != '') {
						$requete .= " AND `gene` LIKE '" . $gene . "'";
					}
					if (isset ( $transcript ) and $transcript != '') {
						$requete .= " AND `transcript` LIKE '%" . $transcript . "%'";
					}
					if (isset ( $modescreening ) and $modescreening != '') {
						$requete .= " AND `modescreening` LIKE '%" . $modescreening . "%'";
					}
					if (isset ( $techniquescreening ) and $techniquescreening != '') {
						$requete .= " AND `techniquescreening` LIKE '%" . $techniquescreening . "%'";
					}
					if (isset ( $cosegregation ) and $cosegregation != '') {
						$requete .= " AND `cosegregation` LIKE '%" . $cosegregation . "%'";
					}
					if (isset ( $diagnosticscreening ) and $diagnosticscreening != '') {
						$requete .= " AND `diagnosticscreening` LIKE '%" . $diagnosticscreening . "%'";
					}
				}
				
				if ($table == 'wholegenomeanalysis') {
					if (isset ( $statutwga ) and $statutwga != '') {
						$requete .= " AND `statut` LIKE '%" . $statutwga . "%'";
					}
					if (isset ( $techniquewga ) and $techniquewga != '') {
						$requete .= " AND `techniquewga` LIKE '%" . $techniquewga . "%'";
					}
					if (isset ( $diagnosticwga ) and $diagnosticwga != '') {
						$requete .= " AND `diagnosticwga` LIKE '%" . $diagnosticwga . "%'";
					}
				}
				
				$requete = ltrim ( $requete, " AND" );
				$requete_finale .= $requete;
				$requete_finale = rtrim ( $requete_finale, " WHERE" );
				$requete_finale .= " ORDER BY `index` ASC";
			}
			$_SESSION ['requete_' . $table] = $requete_finale;
			
			if ($requete_finale != '') {
				echo "<p><br /><b>" . strtoupper ( $table ) . "</b><br />";
				echo "<h0>Request : ", $requete_finale, "</h0></p>";
				echo "<table class='principale' cellspacing='1' cellpadding='3' style='widgth:90%; margin-top:0px; margin-left:0px; margin-right:0px; font-size:10px; position:relative; float:top; cols=27; '>";
				
				$liste_champs = array ();
				echo "<tr style='color:white; background-color:#c0c0c0;'><th>Modify</th><th>Delete</th>";
				$requete_recup_entete = "SELECT * FROM `" . $table . "_nom_champ`
				ORDER BY `ID_index_nom_champ` ASC";
				
				$result_recup_entete = mysql_query ( $requete_recup_entete ) or die ( "Impossible de recupérer les entetes : " . mysql_error () );
				$liste_champs = array ();
				while ( $ligne = mysql_fetch_assoc ( $result_recup_entete ) ) {
					$hash_champ_nomchamp [$ligne ['champ']] = $ligne ['nom_champ'];
					array_push ( $liste_champs, $ligne ['champ'] ); // pas le choix si je veux garder l'ordre ...
				}
				foreach ( $liste_champs as $champ ) {
					echo "<th>", $hash_champ_nomchamp [$champ], "</th>";
				}
				
				echo "</tr>";
				
				$result_genetics = mysql_query ( $requete_finale ) or die ( "Request failed : " . mysql_error () );
				$j = 0;
				while ( $ligne = mysql_fetch_assoc ( $result_genetics ) ) {
					if (count ( $ligne ) != 0) {
						if ($j == 0) {
							echo "<tr style='background-color:#EEF7FC;'>";
							$j = 1;
						} else if ($j == 1) {
							echo "<tr style='background-color:#E6F2F8;'>";
							$j = 0;
						}
						$i = 0;
						
						foreach ( $liste_champs as $champ ) {
							echo "<td style='width:400px; height:30px;'>";
							if ($i == 0 and $champ != '') {
								if ($_SESSION ['acces_genetique'] >= 3) {
									echo "<a href='.." . $_SESSION ['pathway_js'] . "/modif_index.php?ind=$ind&amp;fiche=" . $ligne ['index'] . "&amp;table=$table' style='text-decoration:underline;'><img src='" . $_SESSION ['pathway'] . "/images/modifier.png' height='15' /></a></td>";
									echo "<td><img src='" . $_SESSION ['pathway'] . "/images/supprimer.png' height='15' onclick=\"confirmer_suppression('" . $ligne ['index'] . "', '$table','$ind')\" /></td>";
								} else {
									echo "<img src='" . $_SESSION ['pathway'] . "/images/modifier_grise.png' height='15' /></td>";
									echo "<td><img src='" . $_SESSION ['pathway'] . "/images/supprimer_grise.png' height='15' /></td>";
								}
								if (isset ( $ligne [$champ] ) and $ligne [$champ] != '') {
									echo "<td>", $ligne [$champ], "</td>";
								}
							} elseif (in_array ( $i, $liens [$table] ) and $champ != '') {
								$liste_fichiers = explode ( ";", $ligne [$champ] );
								foreach ( $liste_fichiers as $fichier ) {
									echo "<a href='../genetiquespataxeurospa/uploads/$fichier' target=_blank style='text-decoration:underline;'>$fichier</a> ";
								}
							} else {
								if (isset ( $ligne [$champ] ) and $ligne [$champ] != '') {
									echo $ligne [$champ];
								}
							}
							$i ++;
							echo "</td>";
						}
						echo "</tr>";
					}
				}
				?></table><?php
			}
		}
	} else {
		echo "<table class='principale'><tr><td>Please fill the family code field</td></tr></table>";
	}
}

include ($_SESSION ['pathway'] . "/footer.php");

?>
